*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure VII	 								   *
********************************************************************************

cap log close                  
log using ${log}/FigureVIIabcdef.log, replace 

cap noisily{
foreach belief in "own" "mover"{
	
	if "`belief'" == "own" {
		use "$data/FTp_u__finaldata.dta", clear		
		local errortitle2 "Belief Own Wage Change - Coworker Wage Change"
		local maintitle1 "Wage Change"
		local maintitle2 "Own (Belief) vs. Coworkers (Data)"
	}
	if "`belief'" == "mover" {
		use "$data/FTp__finaldata.dta", clear		
		local errortitle2 "Belief - Actual Coworker Wage Change"
		local maintitle1 "Wage Change of Movers"
		local maintitle2 "Belief and Data"		
	}
			
	* winsorizing mean coworker wage changes
	cap drop akm_actual_ln_mover_change
	winsor mean_delta_ln_wage_mover_2015, gen(akm_actual_ln_mover_change) p(0.02) 
							
	*****************
	* Figure 7 c, d 
	*****************
	
	gen _biastemp = ln_`belief'_change - akm_actual_ln_mover_change				
	winsor _biastemp, gen(biastemp) p(0.02) 				
	su biastemp 
				
	reg biastemp firm_effect, cluster(pid)
		local N_inderror = e(N)
		local beta_inderror = string(round(_b[firm_effect],0.001),"%4.3f")
		local se_inderror = string(round(_se[firm_effect],0.001),"%4.3f")
		local cons = _b[_cons]
		local beta = _b[firm_effect]

	* at bias = 10%, what AKM percentile? 
	local bias10 = (-0.1 - `cons') / `beta' 
	su firm_effect if !missing(biastemp)
		local N_all = r(N)
	su firm_effect if !missing(biastemp) & firm_effect <= `bias10' 
		local N_below = r(N)
		local perc10 = `N_below'/`N_all'
	di "Percentile of workers at 10% error: `perc10'"		
		
	binscatter biastemp firm_effect, ///
		msymbols(circle none) xtitle("AKM Firm Effect" ) ///
		ytitle("Estimation Error" "`errortitle2'") ///
		legend(on order(- "Slope: `beta_inderror' (SE `se_inderror')") ///
		ring(0) size(medsmall) position(11) region(lwidth(none))) ///
		ylabel(#10,grid) xlabel(#10,grid) yline(0, lcolor(grey%30))
		
	if "`belief'" == "own"{
		graph export ${log}/FigureVIIc.pdf, replace			
	}
	if "`belief'" == "mover"{
		graph export ${log}/FigureVIId.pdf, replace			
	}
					
	drop biastemp _biastemp
				
				
	*****************
	* Figure 7 a, b 
	*****************
				
	reg ln_`belief'_change_w firm_effect if akm_actual_ln_mover_change!=., cluster(pid)
		local N_obs_belief = e(N)
		local beta_belief = string(round(_b[firm_effect],0.001),"%4.3f")
		local se_belief = string(round(_se[firm_effect],0.001),"%4.3f")
		local constant_belief = string(round(_b[_cons],0.001),"%4.3f")		

	reg akm_actual_ln_mover_change firm_effect if ln_`belief'_change_w!=., cluster(pid)
		local N_obs_true = e(N)
		local beta_true = string(round(_b[firm_effect],0.001),"%4.3f")
		local se_true = string(round(_se[firm_effect],0.001),"%4.3f")
		local constant_true = string(round(_b[_cons],0.001),"%4.3f")
				
	* formally testing difference in slopes 
	* [suest requires you to initially estimate without clusters]
	eststo clear
	
	reg ln_`belief'_change_w firm_effect if akm_actual_ln_mover_change!=.
	eststo est1
	
	reg akm_actual_ln_mover_change firm_effect if ln_`belief'_change_w!=.
	eststo est2
	
	suest est1 est2, cluster(pid)
	test [est1_mean]firm_effect=[est2_mean]firm_effect
		
	di "mover AKM slope test"
	
	* creating the binscatter
	cap noisily su ln_`belief'_change_w akm_actual_ln_mover_change
	
	cap drop dup
	
	local yvar "yvar"
	local xvar "firm_effect"
	local splitvar "dup"
		
	preserve
		expand 2, gen(dup)
		gen yvar = .
									
		replace yvar = ln_`belief'_change_w if dup==0
		replace yvar = akm_actual_ln_mover_change if dup==1
								
		quietly binscatter `yvar' `xvar', by(`splitvar') savedata("$data/binscdata") replace
	restore
							
	clear
								
	quietly do "$data/binscdata.do"
								
	quietly su `xvar'_by1
	local xmin = r(min)
	local xmax = r(max)
								
	quietly su `xvar'_by2
	if r(min)<`xmin' local xmin = r(min)
	if r(max)>`xmax' local xmax = r(max)
	
	local ymin = -0.3
	local ymax = 0.35
												
	twoway scatter `yvar'_by1 `xvar'_by1, mcolor(navy) msymbol(circle) ///
		|| scatter `yvar'_by2 `xvar'_by2, mcolor(maroon) msymbol(Th) ///
		|| function y=(`beta_belief'*x)+`constant_belief', range(`xmin' `xmax') color(navy) ///
		|| function y=(`beta_true'*x)+`constant_true', range(`xmin' `xmax') color(maroon) lpattern(dash) legend(order(1 "Belief, Slope: `beta_belief' (SE `se_belief')" 2 "Benchmark (Admin. Data), Slope: `beta_true' (SE `se_true')") cols(1) ring(0) position(7) region(lwidth(none)) size(medsmall) region(color(white%0)))  ///
		xtitle("AKM Firm Effect") ytitle("`maintitle1'" "`maintitle2'") ///
		xscale(range(`xmin' `xmax')) yscale(range(`ymin' `ymax')) ylabel(#10,grid) xlabel(#10,grid)			
	
	if "`belief'" == "own"{
		graph export ${log}/FigureVIIa.pdf, replace			
	}
	if "`belief'" == "mover"{
		graph export ${log}/FigureVIIb.pdf, replace			
	}		
							
	cap erase "$data/binscdata.do" 
	cap erase "$data/binscdata.csv"
	
}
}


***************************
* Figure VII e
***************************

* loading SOEP datasets
use "$data/GSOEPIS2019_survey_long.dta", clear 
append using "$data/GSOEPIS2020_survey_long.dta"

gen jahr = syear
drop syear

* narrowing down to relevant analysis sample
merge m:1 pid jahr using "$data/unionsample_finaldata.dta", keepusing(pid)
keep if _merge==3
drop _merge


* merging on firm IDs (for later merge of AKM effects)
merge 1:m pid jahr using ${data}/data_prep_ADIAB_pid_betnr_jahr.dta
drop if _merge == 2
drop _merge

* merging on data on true rank within occupation - this file is created by 4_occupationrank.do
merge m:1 pid using "$data/within_occupation_rank_2019.dta"
drop if _merge==2
drop _merge

* keep individuals with nonmissing subjective and objective rank
keep if salary_less_proba!=.&salary_more_proba!=.&ieb_beruf_kons_num_ventile!=.

* generate occupation percentile rank
gen occupation_pctl_belief = (salary_less_proba + 100-salary_more_proba)/2
gen occupation_pctl_true = ieb_beruf_kons_num_ventile*5 - 2

*** bias about rank in occupation on AKM effects

* merging on AKM effects
merge m:1 betnr using ${orig}/SOEP-ADIAB_7519_v1_akm_estab.dta

drop if _merge==2
drop _merge

winsor feff_2010_2017, gen(firm_effect) p(0.02)

* creating binscatter
gen _error = occupation_pctl_belief-occupation_pctl_true
winsor _error, gen(error) p(0.02) 					
				
reg error firm_effect, cluster(pid)
	local N_obs = e(N)
	local beta = string(round(_b[firm_effect],0.001),"%4.3f")
	local se = string(round(_se[firm_effect],0.001),"%4.3f")
		
binscatter error firm_effect, ///
	msymbols(circle none) xtitle("AKM Firm Effect" ) ///
	ytitle("Estimation Error" "Belief - Actual Rank in Occupation") ///
	legend(on order(- "Slope: `beta' (SE `se')") region(lwidth(none)) pos(8) ring(0)) ///
	ylabel(#10,grid) xlabel(#10,grid) yline(0, lcolor(grey%30))  
graph export ${log}/FigureVIIe.pdf, replace
		

***************************
* Figure VII f
***************************

use "$data/unionsample_finaldata.dta", clear

winsor d_guess_median_pct, gen(d_guess_median_pct_w2) p(0.02) 
replace d_guess_median_pct_w2 = d_guess_median_pct_w2 / 100 

reg d_guess_median_pct_w2 firm_effect, cluster(pid)
	local N_obs = e(N)
	local beta = string(round(_b[firm_effect],0.001),"%4.3f")
	local se = string(round(_se[firm_effect],0.001),"%4.3f")
	
binscatter d_guess_median_pct_w2 firm_effect, ///
	msymbols(circle none) xtitle("AKM Firm Effect" ) ///
	ytitle("Estimation Error" "Belief - Actual Median Wage in Occupation")  /// 
	legend(on order(- "Slope: `beta' (SE `se')") region(lwidth(none)) pos(11) ring(0)) ///		
	ylabel(#10,grid) xlabel(#10,grid) yline(0, lcolor(grey%30))
graph export ${log}/FigureVIIf.pdf, replace


log close
clear


